package com.demo.myapplication.fragment;

import android.animation.StateListAnimator;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.demo.myapplication.R;
import com.demo.myapplication.adapter.RecyclerActionModeAdapter;
import com.demo.myapplication.utils.CommonUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class RecyclerActionModeFragment extends Fragment implements RecyclerActionModeAdapter.MyViewHolder.ClickListener {
  private String Tag = RecyclerActionModeFragment.class.getSimpleName();

  private RecyclerView recycler;
  private RecyclerActionModeAdapter recyclerAdapter;

  private ArrayList<Map<String, Object>> dataList;

  private void initData() {
    dataList = new ArrayList<>();
    for (int i = 0; i < CommonUtils.icon.length; i++) {
      Map<String, Object> map = new HashMap<>();
      map.put(CommonUtils.map_icon_key, CommonUtils.icon[i]);
      map.put(CommonUtils.map_text_key, getString(CommonUtils.iconName[i]));
      dataList.add(map);
    }
  }

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_recycler_action_mode, container, false);
    recycler = (RecyclerView) v.findViewById(R.id.recycler);
    initData();
    setupRecycler();
    return v;
  }

  private void setupRecycler() {
    recyclerAdapter = new RecyclerActionModeAdapter(dataList);
    recyclerAdapter.setListener(this);
    GridLayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 3);
    recycler.setLayoutManager(mLayoutManager);
    recycler.setAdapter(recyclerAdapter);
  }


  private android.support.v7.view.ActionMode mActionMode;

  public static Fragment newInstance(Bundle args) {
    RecyclerActionModeFragment fragment = new RecyclerActionModeFragment();
    fragment.setArguments(args);
    return fragment;
  }

  @Override
  public void onItemClicked(View v, int position, Drawable mDefaultModeBackgroundDrawable, StateListAnimator mDefaultModeStateListAnimator) {
    Log.d("Tag", "position=" + position +",onItemclicked");
    if (mActionMode != null) {
      toggleSelection(v, position, mDefaultModeBackgroundDrawable, mDefaultModeStateListAnimator);
    } else {
      String text = (String) dataList.get(position).get(CommonUtils.map_text_key);
      Snackbar.make(recycler, String.format(getString(R.string.tip_click), text), Snackbar.LENGTH_LONG).show();
    }
  }

  private android.support.v7.view.ActionMode.Callback actionModeCallback = new android.support.v7.view.ActionMode.Callback() {

    @Override
    public boolean onCreateActionMode(android.support.v7.view.ActionMode mode, Menu menu) {
      getActivity().getMenuInflater().inflate(R.menu.fragment_actionmode_menu, menu);
      mode.setTitle(getString(R.string.choose_item));
      mActionMode = mode;
      return true;
    }

    @Override
    public boolean onPrepareActionMode(android.support.v7.view.ActionMode mode, Menu menu) {
      return false;
    }

    @Override
    public boolean onActionItemClicked(android.support.v7.view.ActionMode mode, MenuItem item) {
      return false;
    }

    @Override
    public void onDestroyActionMode(android.support.v7.view.ActionMode mode) {
      SparseBooleanArray selectedItems = recyclerAdapter.getSelectedItems();
      for (int i = 0; i < selectedItems.size(); i++) {
        recycler.getChildAt(selectedItems.keyAt(i)).setActivated(false);
      }
      recyclerAdapter.clearSelection();
      mActionMode = null;
    }
  };

  private void toggleSelection(View v, int position, Drawable mDefaultModeBackgroundDrawable, StateListAnimator mDefaultModeStateListAnimator) {
    recyclerAdapter.toggleSelection(v, position, mDefaultModeBackgroundDrawable, mDefaultModeStateListAnimator);
    int count = recyclerAdapter.getSelectedItemCount();
    if (count == 0) {
      mActionMode.finish();
    } else {
      mActionMode.setSubtitle(String.format(getString(R.string.choosed_item), count));
    }
  }

  @Override
  public boolean onItemLongClicked(View v, int position, Drawable mDefaultModeBackgroundDrawable, StateListAnimator mDefaultModeStateListAnimator) {
    if (mActionMode == null) {
      mActionMode = ((AppCompatActivity) getActivity()).startSupportActionMode(actionModeCallback);
    }
    Log.d("Tag", "onItemLongClicked positon=" + position);
    toggleSelection(v, position, mDefaultModeBackgroundDrawable, mDefaultModeStateListAnimator);
    return true;
  }

}

